//
//  HomePageViewController.m
//  Nidalee
//
//  Created by 李康 on 2020/11/30.
//  Copyright © 2020 李康. All rights reserved.
//

#import "HomePageViewController.h"
#import "SDCycleScrollView.h"
#import <Masonry/Masonry.h>
#import "MarqueeView.h"
#import "HomeMianTableViewCell.h"
#import "HomeSectionView.h"
#import "NewsPageViewController.h"
#import "NewsMianListViewController.h"

@interface HomePageViewController ()<UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate>

//轮播
@property (nonatomic, strong) SDCycleScrollView *bannerView;
//跑马灯视图
@property (nonatomic, strong) MarqueeView *marqueeView;

@property (strong, nonatomic) UITableView *mainTableView;

@property (strong, nonatomic) NSMutableArray *dataArr;

@end

@implementation HomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addLeftTextBarButtonByItemTitle:@"消息"];
    [self addRightTextBarButtonByItemTitle:@"我的评论"];
    [self.view addSubview:self.mainTableView];
    
    UIView *bannerBack = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 180* M6Scale +35)];
    bannerBack.backgroundColor = [UIColor whiteColor];
    //添加banner
    self.bannerView = [[SDCycleScrollView alloc]init];
    self.bannerView.delegate = self;
    [bannerBack addSubview:self.bannerView];
    self.bannerView.placeholderImage = [UIImage imageNamed:@"BHP_banner_placeholder"];
    [self.bannerView mas_makeConstraints:^(MASConstraintMaker *make) {
      make.top.equalTo(bannerBack.mas_top).offset(20 * M6Scale);
      make.centerX.equalTo(bannerBack.mas_centerX);
      make.width.equalTo(@(345 * M6Scale));
      make.height.equalTo(@(140 * M6Scale));
    }];
    self.bannerView.layer.cornerRadius = 5;
    self.marqueeView = [[MarqueeView alloc]initWithFrame:CGRectMake(15*M6Scale, 180* M6Scale, ScreenWidth -30*M6Scale, 35)];
    [bannerBack addSubview:self.marqueeView];
    [self.marqueeView setLeftImageStr:@"marqueeView_tip"];
    [self.marqueeView startAnimation];

    self.mainTableView.tableHeaderView = bannerBack;
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.hidesBottomBarWhenPushed = NO;
    self.marqueeView.contentString = @"本院于2020年11月16日立案受理公益诉讼起诉人甘肃省兰州市人民检察院与被告肖原平、伏上上侵权责任纠纷公益诉讼一案。诉讼过程中，当事人达成调解协议。依照《最高人民法院关于适用<中华人民共和国民事诉讼法>的解释》第二百八十九条规定，现予以公告。公告期30日。";
    [self setNavTitle:@"首页首页"];
}

-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray arrayWithArray:@[@{@"title":@"市场要闻",@"contetArr":@[]},
                                                    @{@"title":@"名家评论",@"contetArr":@[]},
                                                    @{@"title":@"外盘行情",@"contetArr":@[]},
                                                    @{@"title":@"大盘走向",@"contetArr":@[]}]];
    }
    return _dataArr;
}

-(UITableView *)mainTableView
{
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, ScreenWidth, ScreenHeight-TabbarHeight-NavigationBarHeight) style:UITableViewStyleGrouped];
        _mainTableView.showsVerticalScrollIndicator = NO;
        _mainTableView.showsHorizontalScrollIndicator = NO;
        [_mainTableView registerNib:[UINib nibWithNibName:@"HomeMianTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"HomeMianTableViewCell"];
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        _mainTableView.rowHeight = 6;
        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainTableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        if (@available(iOS 11.0, *)) {
            _mainTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }else{
            self.automaticallyAdjustsScrollViewInsets = NO;//scrollView从状态栏开始 不会自适应调整
        }
    }
    return _mainTableView;
}

#pragma mark -- <UITableViewDelegate,UITableViewDataSource>
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static  NSString *cellID = @"HomeMianTableViewCell";
    HomeMianTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[HomeMianTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.text = @"11";
    }
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    NSDictionary *dic = self.dataArr[section];
    HomeSectionView *view = [[HomeSectionView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 50)];
    view.titleString = dic[@"title"];
    view.callBack = ^(id  data) {
        NewsMianListViewController *vc = [[NewsMianListViewController alloc]init];
        vc.index = section;
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        vc.hidesBottomBarWhenPushed = NO;
    };
    view.backgroundColor = HexColor(0xDAA520);
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NewsPageViewController *vc = [[NewsPageViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    vc.hidesBottomBarWhenPushed = NO;
}

//消息
-(void)leftBarAction:(id)sender{
    
}

//我的评论
-(void)rightBarAction:(id)sender{
    
}


@end
